using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Info.Basic;
using EntLibEC.Biz;
using EntLibEC.Biz.Basic;

namespace EntLibEC.View.Basic
{
	/// <summary>
	/// Summary description for Category2Opt.
	/// </summary>
	public partial class Category2Opt : PageBase
	{

		protected EntLibEC.View.Cmn.Category1 ucC1;
		protected EntLibEC.View.Cmn.BiStatus ucStatus;
		protected string bodyOnLoad;

		private int sysno;

	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if ( Request.QueryString["sysno"] != null)
			{
				try
				{
					this.sysno = Convert.ToInt32(Request.QueryString["sysno"].ToString());
				}
				catch
				{
					Assert(lblMessage, "sysno is not an integer", -1);
					return;
				}
			}
			if ( !Page.IsPostBack )
				BindForm();
		}

		private void BindForm()
		{
			ucC1.BindC1(false);
			ucStatus.BindStatus(false);
			Category2Info oInfo = CategoryManager.GetInstance().GetC2Hash()[this.sysno] as Category2Info;
			if ( oInfo == null )
			{
				Assert(lblMessage, "there is no this second category in server memory", -1);
				btnUpdate.Enabled = false;
				return;
			}
			ucC1.C1SysNo = oInfo.C1SysNo;
			txtC2ID.Text = oInfo.C2ID;
			txtC2Name.Text = oInfo.C2Name;
			ucStatus.StatusSysNo = oInfo.Status;

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnUpdate_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.CategoryOpt, false, lblMessage) )
				return;

			if ( !CheckForm())
				return;

			try
			{
				Category2Info oInfo = new Category2Info();
				oInfo.SysNo = this.sysno;
				oInfo.C1SysNo = ucC1.C1SysNo;
				oInfo.C2ID = txtC2ID.Text.Trim();
				oInfo.C2Name = txtC2Name.Text.Trim();
				oInfo.Status = ucStatus.StatusSysNo;
				CategoryManager.GetInstance().Update(oInfo);

				LogManager.GetInstance().Write(new LogInfo(oInfo.SysNo, (int)AppEnum.LogType.Basic_Category2_Update, base.sInfo));
				Assert(lblMessage, "update category second ok", 1);
				bodyOnLoad = "javascript:window.opener.__doPostBack('btnSearch','btnSearch');window.close(); return false;";
			}
			catch(BizException exp)
			{
				Assert(lblMessage, exp.Message, -1);
			}
			catch(Exception exp)
			{
				Assert(lblMessage, "update category second failed, see log", -1);
				ErrorLog.GetInstance().Write(exp.ToString());
			}
		
		}

		private bool CheckForm()
		{
			if ( ucC1.C1SysNo == AppConst.IntNull )
			{
				Assert(lblMessage, "first category can't be empty", -1);
				return false;
			}
			if ( txtC2ID.Text.Trim() == "")
			{
				Assert(lblMessage, "second category ID can't be empty", -1);
				return false;
			}
			if ( txtC2Name.Text.Trim() == "" )
			{
				Assert(lblMessage, "second category name can't be empty", -1);
				return false;
			}
			return true;
		}
	}
}
